/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-08-14
 * Time: 20:28
 */
public class MyDoubleList {
    class Node {
        int data;
        Node next;
        Node prev;

        public Node(int val) {
            this.data = val;
        }
    }

    Node head;
    Node last;

    //打印
    public void display() {
        if (head == null){
            System.out.println("空链表");
            return;
        }
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //size()
    public int size() {
        Node cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    /**
     * 增加
     */
    //头插
    public void addFirst(int val) {
        Node node = new Node(val);
        //空链表
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;

        }
    }

    //尾插
    public void addLast(int val) {
        Node node = new Node(val);
        //空链表
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.prev = last;
            last.next = node;
            last = node;
        }
    }

    //任意位置插
    //检查index是否合法
    private boolean checkIndex(int index) {
        if (index < 0 || index > size()) {
            return false;
        }
        return true;
    }

    public void indexAdd(int index, int val) {
        Node node = new Node(val);
        //空链表
        if (head == null) {
            head = node;
            last = node;
        }
        //检查index是否合法
        if (!checkIndex(index)) {
            throw new RuntimeException("插入时::index不合法");
        }
        //index==0 头插
        if (index == 0) {
            addFirst(val);
            return;
        }
        //index == size()
        if (index == size()) {
            addLast(val);
            return;
        }
        //代码走到这里,index合法,既不是头插也不是尾插
        //双链表有prev 不用我们在手动找前驱
        Node cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;

    }

    /**
     * 修改
     */
    //检查index是否合法
    private boolean checkUddateIndex(int index) {
        if (index < 0 || index >= size()) {
            return false;
        }
        return true;
    }

    public void update(int index, int val) {
        //空链表
        if (head == null) {
            return;
        }
        //检查index是否合法
        if (!checkUddateIndex(index)) {
            throw new RuntimeException("update::index不合法");
        }
        Node cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        cur.data = val;
    }

    /**
     * 删除
     */
    //删除第一次出现的节点
    //双链表删除也不需要找val的前驱
    public void delete(int val) {
        //空链表
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            //找到了val
            if (cur.data == val) {
                //val是头结点
                if (cur == head) {
                    head.next.prev = null;
                    head = head.next;
                    return;
                }
                //val是尾结点
                if (cur == last) {
                    last.prev.next = null;
                    last = last.prev;
                    return;
                }
                //val既不是头结点也不是尾结点
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                return;

            }
            cur = cur.next;

        }
    }
    //删除所有为val的节点
    //让while(cur != null)循环把链表遍历完,遇到val就删除即可
    public void deleteAll(int val) {
        //空链表
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            //找到了val
            if (cur.data == val) {
                //val是头结点
                if (cur == head) {
                    head.next.prev = null;
                    head = head.next;

                }
                //val是尾结点
                if (cur == last) {
                    last.prev.next = null;
                    last = last.prev;

                }
                //val既不是头结点也不是尾结点
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;


            }
            cur = cur.next;

        }
    }
    /**
     *查
     */
    public boolean find(int val){
        //空链表
        if (head == null) {
            return false;
        }
        Node cur = head;
        while (cur != null){
            if(cur.data == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //清空 双链表无法暴力清空
    //把每个节点的next,prev都置空
    public void clean(){
        //空链表
        if (head == null) {
            return ;
        }
        Node cur = head;
        while (cur != null){
            Node curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur=curNext;
        }
        head = null;
        last = null;

    }

}
